function newcoord=changecoord(s1, s2,i,j,h)
  %Take a 3d matrix A with s1=size(A,1), s2=size(A,2), s3=size(A,3)
  %Consider B=reshape(A, s1*s2*s3,1)
  %E.g. 
  %A(:,:,1)=[1 2 3; 4 5 6];
  %A(:,:,2)=[7 8 9; 10 11 12];
  %A(:,:,3)=[13 14 15; 16 17 18];
  %A(:,:,4)=[19 20 21; 22 23 24];
  %B=[1;4;2;5;3;6;7;10;8;11;9;12;...]; 
  %Suppose i=1, j=3, h=2
  %This function gives the position in B of A(1,3,2)
  newcoord = i + ( j-1 + (h-1)*s2 ) * s1;    
end